//
//  JYLoadingView.m
//  s.
//
//  Created by fanshijian on 2022/8/24.
//  Copyright © 2022 J. All rights reserved.
//

#import "JYLoadingView.h"

@interface JYLoadingView ()
@property (weak, nonatomic) UIActivityIndicatorView  *indicatorView;
@end

@implementation JYLoadingView

+ (void)load {
    // 改变loading圈的颜色
    [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[JYLoadingView class]]].color = [UIColor grayColor];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
//        // 背景
//        UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
//        backgroundImageView.backgroundColor = [UIColor clearColor];
//        [self addSubview:backgroundImageView];
//        [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.right.left.mas_equalTo(0);
//        }];
//
//
//        // gif动画
//        UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
//        NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"];
//        NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
//        loadingImageView.image = [UIImage sd_imageWithGIFData:gifData];
//        loadingImageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
//        [self addSubview:loadingImageView];
//        [loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.height.mas_equalTo(80*UI_Scale_Height);
//            make.centerX.equalTo(self.mas_centerX).offset(0);
//            make.centerY.equalTo(self.mas_centerY).offset(0);
//        }];
        
        [self indicatorView];
    }
    return self;
}

- (UIActivityIndicatorView *)indicatorView {
    if (!_indicatorView) {
        UIActivityIndicatorView *obj = [UIActivityIndicatorView new];
        [self addSubview:_indicatorView = obj];
        obj.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        // 改变圈圈大小
        obj.transform = CGAffineTransformMakeScale(2, 2);
    }
    return _indicatorView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.indicatorView.center = self.center;
}

- (void)setShowLoading:(BOOL)showLoading{
    _showLoading = showLoading;
    self.hidden  = !_showLoading;
    if (_showLoading) {
        [self.indicatorView startAnimating];
    }else{
        [self.indicatorView stopAnimating];
    }
}


@end
